import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/erp_odd_numbers_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_details_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/provider/erp_info.dart';
import 'package:xiongan_app_yy/utils/ImageDetailsUtils.dart';

import 'comm/count_button.dart';
import 'modal_widget.dart';

class InTaskMterialListWidget extends StatefulWidget {
  final BuildContext context;
  final dynamic data;

  InTaskMterialListWidget({this.context, @required this.data});

  @override
  State<StatefulWidget> createState() {
    return InTaskMterialListWidgetState();
  }
}

class InTaskMterialListWidgetState extends State<InTaskMterialListWidget> {
  String image = "assets/images/build.png";
  Widget imageWidget;
  String mMaterialno;

  String mName;
  int mQuantity;

  @override
  Widget build(BuildContext context) {
    if (widget.data.imageurl != null&&image=="assets/images/build.png") {
      getImageBase64(widget.data.imageurl);
    }
    imageWidget = isImageType();
    mMaterialno = widget.data.materialcode;
    mName = widget.data.materialname;
    mQuantity = widget.data.qty;
    return Container(
      color: Colors.white,
      width: double.infinity,
      padding: EdgeInsets.all(15),
      margin: EdgeInsets.only(top: 10),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                child: GestureDetector(
                  child: imageWidget,
                  onTap: () {
                    ImageDetailsUtils.jumpImageDetails(
                        context, image, image == "assets/images/build.png");
                  },
                ),
                width: 80,
                height: 80,
                margin: EdgeInsets.only(right: 10),
              ),
              Expanded(
                  child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("实物ID：$mMaterialno",
                      style: TextStyle(fontSize: 13, color: Color(0xff646464))),
                  Text(mName,
                      style: TextStyle(fontSize: 13, color: Color(0xff646464))),
                  Text("数量：$mQuantity",
                      style: TextStyle(fontSize: 13, color: Color(0xff646464)))
                ],
              )),
              GestureDetector(
                child: Icon(
                  Icons.error_outline,
                  size: 25,
                  color: Colors.yellow,
                ),
                onTap: () {
                  print("点击");
                  Navigator.of(context).push(PageRouteBuilder(
                      opaque: false,
                      pageBuilder: (BuildContext context, Animation animation,
                          Animation secondaryAnimation) {
                        return ScaleTransition(
                            scale: Tween<double>(begin: 0.0, end: 1.0).animate(
                                CurvedAnimation(
                                    parent: animation,
                                    curve: Curves.fastOutSlowIn)),
                            child: ModalWidget(widget.data));
                      }));
                },
              )
            ],
          ),
        ],
      ),
    );
  }

  Widget isImageType() {
    if (image == "assets/images/build.png") {
      return Image.asset(image, fit: BoxFit.fill);
    } else {
      return Image.memory(
        //base64.decode(image),
        base64Decode(image),
        height: 80,
        width: 80,
        fit: BoxFit.fill,
        gaplessPlayback: true, //防止重绘
      );
    }
  }

  void getImageBase64(String s) {
    InternetAPI.getInstance().post(
      "FileUpload/File/DownFlie",
      (data) {
        setState(() {
          image = data;
        });
      },
      params: {"filePath": s},
      options: new Options(
          contentType: ContentType.parse("application/x-www-form-urlencoded")),
    );
  }
}
